import React from 'react';

import {Switch,Route,Redirect} from 'react-router-dom';

import {RouteProps} from 'react-router';

import {RouteItem} from './router.d';

const RenderRouterView = function(routes:RouteItem[]){
    if(!routes.length){
        return null
    }
    return <Switch>
        {
            routes.map((item:RouteItem) => {
                if(item.component){
                    let Com = item.component;
                    return <Route key={item.path} path={item.path} render={(props:RouteProps) => {
                        return <Com {...props} routes={item.children ? item.children : []}></Com> 
                    }}></Route>
                }
            })
        }
        {
            routes.map((item:RouteItem) => {
                if(item.redirect){
                    return <Redirect key={item.path} from={item.path} to={item.redirect}></Redirect>
                }
            })
        }
    </Switch>
}

export default RenderRouterView